import { createRouter, createWebHistory } from "vue-router";
import Admin from "../views/Admin.vue";
import Login from "../views/Login.vue";
import List from "../views/user/index.vue";
import Fans from "../views/user/fans.vue";
import Following from "../views/user/following.vue";
import Friend from "../views/user/friend.vue";
import Chat from "../views/user/chat.vue";
import Dynamic from "../views/dynamic/index.vue";
import Topic from "../views/dynamic/topic.vue";
import Comment from "../views/dynamic/comment.vue";
import Report from "../views/dynamic/report.vue";
import Store from "../views/store/index.vue";
import Order from "../views/store/order.vue";
import Banner from "../views/store/banner.vue";
import Activity from "../views/activity/index.vue";
import Riding from "../views/activity/riding.vue";
import WalletLog from "../views/store/walletLog.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      redirect: "/login",
    },
    {
      path: "/admin",
      name: "admin",
      component: Admin,
      children: [
        {
          path: "user/",
          name: "user",
          component: List,
        },
        {
          path: "fans/",
          name: "fans",
          component: Fans,
        },
        {
          path: "following/",
          name: "following",
          component: Following,
        },
        {
          path: "friend/",
          name: "friend",
          component: Friend,
        },
        {
          path: "chat/",
          name: "chat",
          component: Chat,
        },
        {
          path: "dynamic/",
          name: "dynamic",
          component: Dynamic,
        },
        {
          path: "topic/",
          name: "topic",
          component: Topic,
        },
        {
          path: "report/",
          name: "report",
          component: Report,
        },
        {
          path: "store/",
          name: "store",
          component: Store,
        },
        {
          path: "order/",
          name: "order",
          component: Order,
        },
        {
          path: "banner/",
          name: "banner",
          component: Banner,
        },
        {
          path: "walletlog/",
          name: "walletlog",
          component: WalletLog,
        },
        {
          path: "activity/",
          name: "activity",
          component: Activity,
        },
        {
          path: "riding/",
          name: "riding",
          component: Riding,
        },
        {
          path: "comment/",
          name: "comment",
          component: Comment,
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
  ],
});

router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.path === "/login") {
    next();
  } else {
    // 判断是否登录
    // 未登录
    if (!localStorage.getItem("token")) {
      next("/login");
    } else {
      // 已登录
      next();
    }
  }
});

export default router;
